package com.spic.system.service.impl;

import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.SecurityUtils;
import com.spic.common.datascope.annotation.DataScope;
import com.spic.system.domain.SysNoticeInfo;
import com.spic.system.mapper.SysNoticeInfoMapper;
import com.spic.system.service.ISysNoticeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 我的消息Service业务层处理
 *
 * @author spic
 * @date 2021-11-04
 */
@Service
public class SysNoticeInfoServiceImpl implements ISysNoticeInfoService
{
    @Autowired
    private SysNoticeInfoMapper sysNoticeInfoMapper;

    /**
     * 查询我的消息
     *
     * @param id 我的消息主键
     * @return 我的消息
     */
    @Override
    public SysNoticeInfo selectSysNoticeInfoById(Long id)
    {
        return sysNoticeInfoMapper.selectSysNoticeInfoById(id);
    }

    /**
     * 查询我的消息列表
     *
     * @param sysNoticeInfo 我的消息
     * @return 我的消息
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u",menuPath = "system/noticeinfo/index")
    public List<SysNoticeInfo> selectSysNoticeInfoList(SysNoticeInfo sysNoticeInfo)
    {
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getUsername();

        if(userId !=null && userId>0){
            sysNoticeInfo.setUserId(userId+"");
        }

        List<SysNoticeInfo>  sysNoticeInfosList = sysNoticeInfoMapper.selectSysNoticeInfoList(sysNoticeInfo);
        if("admin".equals(username)){
            return sysNoticeInfosList;
        }else{
            List<SysNoticeInfo> sysNoticeInfoNew = new ArrayList<>();
            sysNoticeInfosList = sysNoticeInfoMapper.selectSysNoticeInfoList(sysNoticeInfo);
            if(sysNoticeInfosList!=null && sysNoticeInfosList.size()>0){
                for (SysNoticeInfo noticeInfo : sysNoticeInfosList)
                {
                    String noticeUserId = noticeInfo.getUserId();
                    if(noticeUserId.equals(userId+"")){
                        sysNoticeInfoNew.add(noticeInfo);
                    }
                }
            }
            return sysNoticeInfoNew;
        }
    }
    /**
     * 新增我的消息
     *
     * @param sysNoticeInfo 我的消息
     * @return 结果
     */
    @Override
    public int insertSysNoticeInfo(List<SysNoticeInfo> sysNoticeInfo)
    {
        if(CollectionUtils.isEmpty(sysNoticeInfo)) return 0;
        Date now = DateUtils.getNowDate();
        sysNoticeInfo.forEach(info->{
            info.setCreateBy("1");
            info.setCreateTime(now);
            info.setIsRead("0");
            info.setStatus("1");
        });
        return sysNoticeInfoMapper.batchInsert(sysNoticeInfo);
    }



    /**
     * 修改我的消息
     *
     * @param sysNoticeInfo 我的消息
     * @return 结果
     */
    @Override
    public int updateSysNoticeInfo(SysNoticeInfo sysNoticeInfo)
    {
        sysNoticeInfo.setUpdateTime(DateUtils.getNowDate());
        return sysNoticeInfoMapper.updateSysNoticeInfo(sysNoticeInfo);
    }

    /**
     * 批量删除我的消息
     *
     * @param ids 需要删除的我的消息主键
     * @return 结果
     */
    @Override
    public int deleteSysNoticeInfoByIds(Long[] ids)
    {
        return sysNoticeInfoMapper.deleteSysNoticeInfoByIds(ids);
    }

    /**
     * 删除我的消息信息
     *
     * @param id 我的消息主键
     * @return 结果
     */
    @Override
    public int deleteSysNoticeInfoById(Long id)
    {
        return sysNoticeInfoMapper.deleteSysNoticeInfoById(id);
    }

    @Override
    public int selectIsReadCount()
    {
        String username = SecurityUtils.getUsername();
        Long userId = SecurityUtils.getUserId();
        if("admin".equals(username)){
            return sysNoticeInfoMapper.selectIsReadCount("");
        }else{
            return sysNoticeInfoMapper.selectIsReadCount(userId+"");
        }
    }
}
